package com.uh.microservice.core.spring;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * spring容器工具类，通过静态方法向外开放
 * Created by liming on 2017/9/18.
 */
@Component
public class SpringUtil {
    private static ApplicationContext app = null;

    public static ApplicationContext getApp() {
        return app;
    }

    public static void setAPP(ApplicationContext context){
        app=context;
    }

    public static Object getBean(String name) {
        return getApp().getBean(name);
    }

    /**
     *  通过Class类型来获取指定的Bean实例
     */
    public static <T> T getBean(Class<T> clazz) {
        try {
            return getApp().getBean(clazz);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     *  通过name,以及Class类型来获取指定的Bean实例
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApp().getBean(name, clazz);
    }

    /**
     * 通过名称来获取yml中属性值
     */
    public static String getProp(String prop){
        PropVarb pv=(PropVarb)SpringUtil.getBean("propvarb");
        return pv.getProp(prop);
    }

    /**
     * 判断yml中是否包含属性名
     */
    public static Boolean containProp(String prop){
        String autostart=getProp(prop);
        if(StringUtils.isBlank(autostart)) return false;
        return true;
    }

    /**
     * 判断程序是否处于debug模式下
     */
    public static boolean isDebug(){
        String mode=getProp("spring.application.app_mode");
        if(StringUtils.equalsIgnoreCase(mode,"DEBUG"))
            return true;
        return false;
    }

}

